package models

import (
	"emicro_test/emicro_2/grpc_server/user/common"
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"gorm.io/gorm/schema"
	"log"
)

var Db *gorm.DB

func init(){
	addr := common.Config.String("mysql_addr")
	port, _ := common.Config.Int("mysql_port")
	user := common.Config.String("mysql_user")
	pwd := common.Config.String("mysql_pwd")
	db_name := common.Config.String("mysql_db_name")
	dsn := fmt.Sprintf("%v:%v@tcp(%v:%d)/%v?charset=utf8mb4&parseTime=True&loc=Local",
		user, pwd, addr, port, db_name)
	Db1, err := gorm.Open(mysql.Open(dsn), &gorm.Config{
		NamingStrategy: schema.NamingStrategy{
			SingularTable: true,  //禁用表名复数
		},
	})
	if err!=nil{
		log.Println("Init Gorm Err:", err)
	}
	log.Println("Init Success")
	Db = Db1
}
